<?php

class Benefit extends BaseEntity{

	public static $types = array(1 => "benefit:type:discountinxequal", 2 => "benefit:type:date", 3 => "benefit:type:stock", 4 => "benefit:type:percentage");

	function __construct($id = 0){
		$this->logicDelete = true;
		parent::__construct($id);
	}

	function returnOrderByDistance($benefits, $lon, $lat){
		foreach ($benefits as $benefit){
			$benefit->distance = round(distance($benefit->attributes->latitude, $benefit->attributes->longitude, $lat, $lon, 'K'), 0);
		}

		usort($benefits, array("Benefit", "orderByDistance"));
		return $benefits;
	}

	function returnOrderByLikeThenDistance($benefits, $lon, $lat){
		foreach ($benefits as $benefit){
			$benefit->distance = round(distance($benefit->attributes->latitude, $benefit->attributes->longitude, $lat, $lon, 'K'), 0);
		}

		usort($benefits, array("Benefit", "orderByLikeThenDistance"));
		return $benefits;
	}

	function getByCategoryId($lon = null, $lat = null, $cid = -1, $orderBy = "", $limit = 0, $offset = 0, $count = false){
		$where = " startDateTime <= " . time() . " and endDateTime >= " . time();
		if($cid > 0){
			$where .= " and idCategory = " . $cid;
		}

		$benefits = $this->search($where, $orderBy, $limit, $offset, $count);

		if($lon && $lat){
			$benefits = $this->returnOrderByDistance($benefits, $lon, $lat);
		}

		return $benefits;
	}

	function getByText($lon = null, $lat = null, $text = "", $orderBy = "", $limit = 0, $offset = 0, $count = false){
		$where = " (name like '%" . $text . "%' or description like '%" . $text . "%' or company like '%" . $text . "%') ";
		$where .= " and startDateTime <= " . time() . " and endDateTime >= " . time();

		$benefits = $this->search($where, $orderBy, $limit, $offset, $count);

		if($lon && $lat){
			$benefits = $this->returnOrderByDistance($benefits, $lon, $lat);
		}

		return $benefits;
	}

	function getTopRanked($startDate = null, $endDate = null, $lon = null, $lat = null, $limit = 0, $offset = 0, $count = false){
		if(!$endDate){
			$endDate = time();
		}
		$where = " b.startDateTime <= " . $endDate;
		if($startDate){
			$where .= " and b.startDateTime >= " . $startDate;
		}

		$where .= " and b.isDeleted = 0";
		$from = " from Benefit b INNER JOIN Likes l on l.benefitId = b.id ";
		$from .= " INNER JOIN User u on u.id = l.userId and u.isDeleted = 0 ";
		if($count){
			$sql = " select count(distinct b.id) as count ";
			$sql .= $from;
			$sql .= "where " . $where;

			return $this->repository->getValue($sql, "count");
		}else{
			$sql =" select b.id, b.endDateTime, b.name, b.description, b.country, b.province, b.city, b.address, b.number, b.latitude, b.longitude, b.company, b.idType, b.idCategory, b.icon, count(*) as likes ";
			$sql .= $from;
			$sql .= " where " . $where;
			$sql .= " group by b.id, b.endDateTime, b.name, b.description, b.country, b.province, b.city, b.address, b.number, b.latitude, b.longitude, b.company, b.idType, b.idCategory, b.icon";
			$sql .= " order by count(*) desc";
			$limitOff = $this->repository->getLimitOffset($limit, $offset);
			$sql .= $limitOff;

			$benefits = $this->repository->fetchAllObject($sql, get_class($this));

			if($lon && $lat){
				$benefits = $this->returnOrderByLikeThenDistance($benefits, $lon, $lat);
			}

			return $benefits;
		}




	}

	static function orderByDistance($a, $b){
		 if ($a->distance == $b->distance) {
        		return 0;
    	}
    	return ($a->distance < $b->distance) ? -1 : 1;
	}

	static function orderByLikeThenDistance($a, $b){

		if ($a->attributes->likes == $b->attributes->likes) {
			if ($a->distance == $b->distance) {
				return 0;
			}
			return ($a->distance < $b->distance) ? -1 : 1;
		}
		return ($a->attributes->likes > $b->attributes->likes) ? -1 : 1;
	}

	function getFullAddress(){
		$province = getProvince($this->attributes->province);
		return $this->attributes->getHTML("address") . " " . $this->attributes->getHTML("number") . ", " . $this->attributes->getHTML("city") . ", " . $province;
	}

	function validateRequired(){

		if($this->attributes->startDateTime > $this->attributes->endDateTime){
			logError(langEcho("benefit:startbigger"));
			return false;
		}

		$where = " name ='" . $this->attributes->name . "' ";
		$where .= " AND company ='" . $this->attributes->company . "' ";
		$where .= " AND startDateTime ='" . $this->attributes->startDateTime . "' ";
		$where .= " AND endDateTime ='" . $this->attributes->endDateTime . "' ";
		$where .= " AND idType ='" . $this->attributes->idType . "' ";
		$where .= " AND idCategory ='" . $this->attributes->idCategory . "' ";

		$duplicate = new Benefit();
		$duplicate->getByPattern($where);

		if($duplicate->attributes && $this->attributes->id != $duplicate->attributes->id){
			logError(langEcho("benefit:duplicate"));
			return false;
		}

		$location = getLocationFromAddress($this->getFullAddress());
		if(!$location){
			logError(langEcho("benefit:address:bad"));
			return false;
		}
		$this->attributes->latitude = $location["lat"];
		$this->attributes->longitude = $location["lng"];

		return true;

	}
}
?>